package com.yonyou.iuap.controller.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yonyou.iuap.pub.JsonUtils;
import com.yonyou.iuap.shell.entity.KmSettingEntity;
import com.yonyou.iuap.shell.entity.exp.KmSettingQueryPoJo;
import com.yonyou.iuap.shell.pub.RequestDto;
import com.yonyou.iuap.shell.pub.ResultDto;
import com.yonyou.iuap.shell.service.impl.KmSettingServiceImpl;
import com.yonyou.iuap.shell.service.intf.IIndustryChartService;
import com.yonyou.iuap.shell.service.intf.IKmSettingService;

@Controller
@RequestMapping("/kmsetting")
public class KmSettingEntityController {
	private final Logger logger = (Logger) LoggerFactory.getLogger(getClass());
	private long start,end;
	@Autowired
	private IKmSettingService kmSettingImpl;
	@Autowired
	private IIndustryChartService industryChartServiceImpl;
	
	@RequestMapping(value="/App/queryAllIndestryByUser")
	public @ResponseBody String queryAllIndestryByUser(@RequestBody HashMap parMap,HttpServletRequest request,HttpServletResponse response){
		start=System.currentTimeMillis();
		ResultDto resDto = new ResultDto();
		RequestDto reqDto = new RequestDto();
		reqDto.setPk_user(parMap.get("pk_user")==null?null:parMap.get("pk_user").toString());
		resDto = kmSettingImpl.queryAllIndustryByUser(reqDto);
		String rs_json =JsonUtils.toJsonStr(resDto);
		end=System.currentTimeMillis();
		logger.info("/App/querypublish接口调用时间"+String.valueOf(end-start));
		return rs_json; 
	}
	@RequestMapping(value="/App/insertUserSetting")
	public @ResponseBody String insertUserSetting(@RequestBody HashMap parMap,HttpServletRequest request,HttpServletResponse response){
		start=System.currentTimeMillis();
		ResultDto resDto = new ResultDto();
		//构建vo对象
		List<KmSettingEntity> listvo = buildSettingEntity(parMap);
		resDto = kmSettingImpl.insertUserSetting(listvo);
		String rs_json =JsonUtils.toJsonStr(resDto);
		end=System.currentTimeMillis();
		logger.info("/App/querypublish接口调用时间"+String.valueOf(end-start));
		return rs_json;
	}
	private List<KmSettingEntity> buildSettingEntity(Map map){
		//获取选择的行业
		List<String> industryList = (List<String>) map.get("industry");
		//获取选择的设备
		List<String>  partsList  = (List<String>) map.get("parts");
		String pk_user = map.get("pk_user").toString();
		List<KmSettingEntity> listvo = new ArrayList<KmSettingEntity>();
		if(industryList!=null && industryList.size()>0){
			for(String pk : industryList){
				KmSettingEntity vo = new KmSettingEntity();
				vo.setPk_user(pk_user);
				vo.setPk_industry_chart(pk);
				vo.setType("0");
				listvo.add(vo);
			}
		}
		if(partsList!=null && partsList.size()>0){
			for(String pk : partsList){
				KmSettingEntity vo = new KmSettingEntity();
				vo.setPk_user(pk_user);
				vo.setPk_industry_chart(pk);
				vo.setType("1");
				listvo.add(vo);
			}
		}
		return listvo;
	}
	@RequestMapping(value="/App/queryUserSettingNew")
	@ResponseBody
	public String queryUserSettingNew(@RequestBody HashMap parMap,HttpServletRequest request,HttpServletResponse response){
		start=System.currentTimeMillis();
		ResultDto rsDto = industryChartServiceImpl.queryHyGxSbByUserSet(parMap);
		end=System.currentTimeMillis();
		logger.info("/App/querypublish接口调用时间"+String.valueOf(end-start));
		return JsonUtils.toJsonStr(rsDto);
//		return rsDto;
	}
	@RequestMapping(value="/App/queryHyGxSbInfo")
	@ResponseBody
	public String queryHyGxSbInfo(@RequestBody HashMap parMap,HttpServletRequest request,HttpServletResponse response){
		start=System.currentTimeMillis();
		ResultDto rsDto = industryChartServiceImpl.queryHyGxSbByHyPK(parMap);
		end=System.currentTimeMillis();
		logger.info("/App/querypublish接口调用时间"+String.valueOf(end-start));
		return JsonUtils.toJsonStr(rsDto);
	}
	@RequestMapping(value="/App/queryUserSetting")  
	public @ResponseBody String queryUserSetting(@RequestBody HashMap parMap,HttpServletRequest request,HttpServletResponse response) {
		start=System.currentTimeMillis();
		RequestDto resDto = new RequestDto();
		resDto.setPk_user(parMap.get("pk_user").toString());
		ResultDto rsDto = kmSettingImpl.querySettingByUser(resDto); 
		//构建前台需要的json格式
		if(rsDto.getStatus()==0){
			getUserSettingJson(rsDto);
		}
		//System.out.println(rsDto.getData().toString());
		String rtn_json = JsonUtils.toJsonStr(rsDto);
		end=System.currentTimeMillis();
		logger.info("/App/querypublish接口调用时间"+String.valueOf(end-start));
		return rtn_json;  
	}
	private void getUserSettingJson(ResultDto rsDto){
		if(rsDto.getData()!=null){
			try {
				List<KmSettingQueryPoJo> listvo = (List<KmSettingQueryPoJo>) rsDto.getData();
				if(listvo!=null && listvo.size()>0){
					Map<String, KmSettingQueryPoJo> cateMap = new HashMap<String, KmSettingQueryPoJo>();
					List<KmSettingQueryPoJo> pojoList = new ArrayList<KmSettingQueryPoJo>();
					//处理为0的节点
					for(KmSettingQueryPoJo pojo : listvo){
						if(pojo.getType()==0 && cateMap.get(pojo.getCate_code())==null){
							List<KmSettingQueryPoJo> liststr = new ArrayList<KmSettingQueryPoJo>();
							KmSettingQueryPoJo pojo1 = new KmSettingQueryPoJo();
							pojo1.setCate_code(pojo.getCate_code());
							pojo1.setCate_name(pojo.getCate_name());
							pojo1.setCateList(liststr);
							pojoList.add(pojo1);
							cateMap.put(pojo.getCate_code(), pojo1);
						}
					}
					//处理部位0的节点
					for(KmSettingQueryPoJo pojo : listvo){
						if(pojo.getType()!=0){
							String key1 = pojo.getCate_code().substring(0,3);
							KmSettingQueryPoJo catePojo = cateMap.get(key1);
							if(catePojo!=null ){
								KmSettingQueryPoJo pojonew = new KmSettingQueryPoJo();
								pojonew.setCate_code(pojo.getCate_code());
								pojonew.setCate_name(pojo.getCate_name());
								pojonew.setPk_industry_chart(pojo.getPk_industry_chart());
								catePojo.getCateList().add(pojonew);
							}
						}
					}
					rsDto.setData(pojoList);
				}
			} catch (Exception e) {
				e.printStackTrace();
				rsDto.setStatus(1);
				rsDto.setMessage("数据格式化异常！");
			}
		}
	}
}
